package com.alon.spring01.study;


import com.alibaba.fastjson.JSON;
import java.math.BigInteger;

import lombok.Data;
import net.sf.cglib.beans.BeanCopier;
import net.sf.cglib.beans.BeanMap;
import net.sf.cglib.beans.BulkBean;
import net.sf.cglib.core.Converter;
import net.sf.cglib.core.DebuggingClassWriter;

/**
 * @program: spring01
 * @description: beanCopier 测试
 * @author: yulongguang
 * @create: 2019-05-14 14:19
 **/
public class BeanCopierTest {

    public static void main(String args[]) {
//        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "/home/ljh/cglib");
//        String[] getter = new String[] { "getValue" };
//        String[] setter = new String[] { "setValue" };
//        Class[] clazzs = new Class[] { int.class };
//
//        BulkBean bean = BulkBean.create(BulkSource.class, getter, setter, clazzs);
//        BulkSource obj = new BulkSource();
//        obj.setValue(1);
//
//        Object[] objs = bean.getPropertyValues(obj);
//        for (Object tmp : objs) {
//            System.out.println(tmp);
//        }

        // 构造
        Pojo pojo = new Pojo();
        pojo.setIntValue(1);
        pojo.setBigInteger(new BigInteger("2"));
        // 初始化
        BeanMap map = BeanMap.create(pojo);

        // 赋值
//        map.setBean(pojo);
        // 验证
        System.out.println(map.get("intValue"));
        System.out.println(map.keySet());
        System.out.println(map.values());
        System.out.println(JSON.toJSONString(map));
//        BeanMap.putAll(map);
    }
}
@Data
class BulkSource {
    private int value;

}
@Data
class Pojo {

    private int        intValue;
    private BigInteger bigInteger;

}

class BigIntConverter implements net.sf.cglib.core.Converter {

    @Override
    public Object convert(Object value, Class target, Object context) {
        System.out.println(value.getClass() + " " + value); // from类中的value对象
        System.out.println(target); // to类中的定义的参数对象
        System.out.println(context.getClass() + " " + context); // String对象,具体的方法名
        if (target.isAssignableFrom(BigInteger.class)) {
            return new BigInteger(value.toString());
        } else {
            return value;
        }
    }
}
